
const cheerio = require('cheerio');



const fs = require('fs')
const request = require('superagent');
require('superagent-charset')(request)

// 启动log
const log4js = require('log4js');
log4js.configure({
    appenders: { cheese: { type: 'file', filename: './FX.log' } },
    categories: { default: { appenders: ['cheese'], level: 'trace' } }
});
const logger = log4js.getLogger('cheese');

var SDlist = [], downLoadCount = 0;

var ServerCookie = "_ga=GA1.2.1665218368.1545923643; _gid=GA1.2.1055855379.1545923643; x8x_smile=3D1; x8x_cookietime=2592000; x8x_auth=oHtLVsnhKlydy8akhK37ESKxaM1HC5Bm2qwzpEzc1flTXZ9uHPFVprcn2tzEgHY; x8x_visitedfid=79D67; x8x_sid=9sr41y; _pk_ses.1.1f00=*; x8x_oldtopics=D82209D82218D82213D82217D82211D82221D81832D81831D82215D82194D82177D81883D82203D81679D81511D82202D82175D81838D82186D81385D81514D82219D82189D82187D; _gat=1; x8x_fid79=1545975119; _pk_id.1.1f00=9521cd314935c10f.1545923645.4.1545975120.1545973649."

let url = 'https://www.yukict.com/bbs/forumdisplay.php?fid=79&page=1';
var page = 1, maxpage = 26
startGet(page);

function startGet(page) {
    console.log(`开始获取第${page}页`)
    logger.info(`开始获取第${page}页`)
    // 一共26页
    request.get(`https://www.yukict.com/bbs/forumdisplay.php?fid=79&page=${page}`)
        .then(res => {
            logger.info(`获取page${page}ok`)
            const $ = cheerio.load(res.text)
            // fs.writeFileSync('page1.html', res.text)
            let robotCount = $("#forum_79").children("tbody").length;
            let tmpCount = 0;
            $("#forum_79").children("tbody").each((index, ele) => {
                let url = $($(ele).find('a')[0]).attr('href');
                robotUrl = `https://www.yukict.com/bbs/${url}`
                getOneData(robotUrl)
                    .then(res => {
                        // 获取一台机体成功
                        console.log(`获取${res.nameCN_F}成功,${tmpCount}`)
                        logger.info(`获取${res.nameCN_F}成功,${tmpCount}`)
                        SDlist.push(res)
                        // 取完一页的，再取下一页；
                        if (++tmpCount == robotCount) {
                            if (page == maxpage) {
                                // 存储机体数据
                                fs.writeFile('./SDdatatmp.json', JSON.stringify(SDlist), function (err) {
                                    if (err) {
                                        console.log(err)
                                    }
                                    console.log('success')
                                })
                            } else {
                                startGet(++page)
                            }
                        }
                    })
            })
        })
        .catch(err => {
            startGet(page)
        })
}

function getOneData(url) {
    return new Promise(function (resolve, reject) {
        request.get(url)
            .set('Cookie', ServerCookie)
            .then(res => {
                const $ = cheerio.load(res.text)
                let data = $($(".postmessage.defaultpost")[0]).html()
                // let str = $($(".postmessage.defaultpost")[0]).find('head1').text()
                let robotName = $($($(".postmessage.defaultpost")[0]).find("h2")).text()
                let nameCN_F = robotName.split("/")[0].trim()
                let nameEN = robotName.split("/")[1].trim()
                let robotpicPath = $($($(".postmessage.defaultpost")[0]).find('img')).attr('src')
                let avatarUrl = `https://www.yukict.com/bbs/${robotpicPath}`
                // 存img
                request.get(avatarUrl)
                    .then(res => {
                        let num = robotpicPath.match(/\d+\.png/)
                        // fs.writeFileSync(`./fxImg/${num}`, res.text)
                        fs.writeFile(`./fxImg/${num}`, res.body, (err) => {
                            if (err) {
                                logger.info(`存储图片失败，编号${num}`)
                            }
                            logger.info(`头像存储-${num}成功`)
                        
                            resolve({
                                nameCN_F: nameCN_F,
                                nameEN: nameEN,
                                FXdata: data,
                                avatarUrl: avatarUrl
                            })
                        })
                    })

            })
            .catch(err => {
                console.log(`机体获取失败，号码${url}，重新获取`)
                logger.error(`机体获取失败，号码${url}`)
                getOneData(url)
            })
    })
}